// 全局变量和常量
const API_BASE_URL = '/api/v1/delivery';
let currentDeliveryPerson = null;
let currentOrderNumber = null;
let mapInstance = null;
let currentLocationMapInstance = null;
let orderRouteMapInstance = null;

// DOM 元素引用
const myOrdersView = document.getElementById('myOrdersView');
const orderDetailView = document.getElementById('orderDetailView');
const profileView = document.getElementById('profileView');
const ordersList = document.getElementById('ordersList');
const ordersLoader = document.getElementById('ordersLoader');
const noOrdersMessage = document.getElementById('noOrdersMessage');
const orderDetailsCard = document.getElementById('orderDetailsCard');
const logisticsTraces = document.getElementById('logisticsTraces');
const updateStatusForm = document.getElementById('updateStatusForm');
const newStatusSelect = document.getElementById('newStatus');
const locationInput = document.getElementById('location');
const descriptionInput = document.getElementById('description');
const backToOrdersBtn = document.getElementById('backToOrdersBtn');
const myOrdersLink = document.getElementById('myOrdersLink');
const myProfileLink = document.getElementById('myProfileLink');
const profileInfo = document.getElementById('profileInfo');
const deliveryPersonName = document.getElementById('deliveryPersonName');
const deliveryPersonStatus = document.getElementById('deliveryPersonStatus');
const statusIndicator = document.getElementById('statusIndicator');
const statusDropdown = document.getElementById('statusDropdown');
const updateLocationBtn = document.getElementById('updateLocationBtn');
const alertArea = document.getElementById('alertArea');
const mainLoader = document.getElementById('mainLoader');
const availableOrdersView = document.getElementById('availableOrdersView');
const availableOrdersList = document.getElementById('availableOrdersList');
const availableOrdersLoader = document.getElementById('availableOrdersLoader');
const noAvailableOrdersMessage = document.getElementById('noAvailableOrdersMessage');
const availableOrdersLink = document.getElementById('availableOrdersLink');

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 初始化页面
    initPage();

    // 事件监听器
    myOrdersLink.addEventListener('click', showMyOrdersView);
    myProfileLink.addEventListener('click', showProfileView);
    availableOrdersLink.addEventListener('click', showAvailableOrdersView);
    backToOrdersBtn.addEventListener('click', showMyOrdersView);
    updateStatusForm.addEventListener('submit', handleUpdateStatus);
    updateLocationBtn.addEventListener('click', updateCurrentLocation);

    // 状态下拉菜单点击事件
    document.querySelectorAll('.dropdown-item').forEach(item => {
        item.addEventListener('click', function(e) {
            e.preventDefault();
            const status = this.getAttribute('data-status');
            updateDeliveryPersonStatus(status);
        });
    });
});

// 初始化页面
async function initPage() {
    showLoader(true);
    try {
        // 获取当前配送员信息
        await fetchDeliveryPersonInfo();
        // 初始化当前位置地图
        initCurrentLocationMap();
        // 加载订单列表
        await loadMyOrders();
        // 加载可接单订单列表
        await loadAvailableOrders();
        // 连接WebSocket
        connectWebSocket();
        // 默认显示订单列表视图
        showMyOrdersView();
    } catch (error) {
        console.error('初始化页面失败:', error);
        showAlert('初始化页面失败，请刷新页面重试。', 'danger');
    } finally {
        showLoader(false);
    }
}